{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "对于回归问题，最简单的检测误差方法就是：Mean Absolute Error（平均绝对误差法），以下为公式："
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$MAE = \\frac{\\sum \\limits_{i=1}^n \\left | predicted_i -actual_i \\right|}{total \\, prediction} $"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们通过Python实现以上公式"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def calculate_the_MAE(predicted_data,actual_data):\n",
    "    the_sum_of_error=0\n",
    "    for i in range(len(actual_data)):\n",
    "        the_sum_of_error += abs(predicted_data[i]-actual_data[i])\n",
    "    MAE=the_sum_of_error/float(len(actual_data))\n",
    "    return MAE"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "利用数据检测结果"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.3\n"
     ]
    }
   ],
   "source": [
    "actual_data   =[1,2,3,4,5,6,7,8,9,10]\n",
    "predicted_data=[2,4,3,5,4,6,5,7,6,8,13]\n",
    "\n",
    "Mean_Absolute_Error=calculate_the_MAE(predicted_data,actual_data)\n",
    "print(Mean_Absolute_Error)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "但是MAE的方法可能会有分类讨论的情况，我们一般不太用。我们可以用Root Mean Squared Error"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "$RMSE=\\sqrt\\frac{\\sum \\limits_{i=1}^n(predicted_i - actual_i)^2}{total \\, prediction}$"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "我们通过Python实现RMSE"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "from math import sqrt\n",
    "def calculate_the_RMSE(predicted_data,actual_data):\n",
    "    the_sum_of_error=0\n",
    "    for i in range(len(actual_data)):\n",
    "        prediction_error=predicted_data[i]-actual_data[i]\n",
    "        the_sum_of_error += (prediction_error**2)\n",
    "    RMSE=sqrt(the_sum_of_error/float(len(actual_data)))\n",
    "    return RMSE"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1.5811388300841898\n"
     ]
    }
   ],
   "source": [
    "actual_data   =[1,2,3,4,5,6,7,8,9,10]\n",
    "predicted_data=[2,4,3,5,4,6,5,7,6,8,13]\n",
    "RMSE=calculate_the_RMSE(predicted_data,actual_data)\n",
    "print(RMSE)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
